﻿using System;
using System.IO;
using System.Linq;
using System.Text;

namespace JoinBoxCurrency
{
    public static partial class StringTool
    {
        // https://blog.csdn.net/my98800/article/details/51450696
        // https://blog.csdn.net/lishuangquan1987/article/details/53678215
        // https://www.cnblogs.com/hont/p/5412340.html  
#if true
        // StringTool.GetRelativePath(@"D:\MyProj\Release", @"D:\MyProj\Log\MyFile.txt");
        // StringTool.GetRelativePath("G:\\A1.项目\\20190920金山谷黄宅\\01.饰施图\\03.平面图", "G:\\A1.项目\\20190920金山谷黄宅\\01.饰施图\\01.辅助文件\\图框\\A3图框.dwg");
        /// <summary>
        /// 绝对路径->相对路径 
        /// </summary>
        /// <param name="strDbPath">绝对路径</param>
        /// <param name="strXrefPath">相对关系</param>
        /// <returns></returns>
        public static string GetRelativePath(string strDbPath, string strXrefPath)
        {
            string[] strDbPaths = strDbPath.Split('\\');
            string[] strXrefPaths = strXrefPath.Split('\\');
            //获取两条路径中的最短路径
            int length = strDbPaths.Length < strXrefPaths.Length ?
                strDbPaths.Length : strXrefPaths.Length;
            //用于确定我们退出的循环中的位置。
            int lastCommonRoot = -1;
            int index;
            //找到共根
            for (index = 0; index < length; index++)
            {
                if (strDbPaths[index] == strXrefPaths[index])
                {
                    lastCommonRoot = index;
                }
                else
                {
                    break;
                }
            }
            //如果我们没有找到一个共同的前缀，那么抛出
            if (lastCommonRoot == -1)
            {
                throw new ArgumentException("路径没有公共相同路径部分");
            }
            //建立相对路径
            var relativePath = new StringBuilder();
            for (index = lastCommonRoot + 1; index < strDbPaths.Length; index++)
            {
                if (strDbPaths[index].Length > 0)
                {
                    relativePath.Append("..\\");//上级目录加入
                }
            }
            //添加文件夹
            for (index = lastCommonRoot + 1; index < strXrefPaths.Length - 1; index++)
            {
                relativePath.Append(strXrefPaths[index] + "\\");
            }
            //本级目录
            if (relativePath.Length == 0)
            {
                relativePath.Append(".\\");
            }
            relativePath.Append(strXrefPaths[strXrefPaths.Length - 1]);//下级目录加入
            return relativePath.ToString();
        }
#else
        /// <summary>
        /// 绝对路径->相对路径
        /// StringTool.GetRelativePath(@"D:\MyProj\Release", @"D:\MyProj\Log\MyFile.txt");
        /// StringTool.GetRelativePath("G:\\A1.项目\\20190920金山谷黄宅\\01.饰施图\\03.平面图", "G:\\A1.项目\\20190920金山谷黄宅\\01.饰施图\\01.辅助文件\\图框\\A3图框.dwg");
        /// </summary>
        /// <param name="strDbPath">绝对路径</param>
        /// <param name="strXrefPath">相对关系</param>
        /// <returns></returns>
        public static string GetRelativePath(string strDbPath, string strXrefPath)
        {
            Uri uri1 = new Uri(strXrefPath);
            Uri uri2 = new Uri(strDbPath);
            Uri relativeUri = uri2.MakeRelativeUri(uri1);//测试例子变成 01.%E8%BE%85%E5%8A%A9%E6%96%87%E4%BB%B6/%E5%9B%BE%E6%A1%86/A3%E5%9B%BE%E6%A1%86.dwg
            string str = relativeUri.ToString();
              
            //因为这里不会实现".\A.dwg"而是"A.dwg",所以加入这个操作,满足同目录文件
            var strs = str.Split('\\');
            if (strs.Length == 1)
            {
                str = ".\\" + str;
            }
            return str;
        }
#endif

        /// <summary>
        /// 相对路径->绝对路径
        /// </summary>
        /// <param name="path_dwg">dwg基础路径</param>
        /// <param name="xiangdui">相对路径</param> 
        /// <returns>完整路径</returns>
        public static string GetCompletePath(string path_dwg, string xiangdui)
        {
            string re_path_opend;
            var path_dwgs = path_dwg.Split('\\');
            path_dwgs = path_dwgs.Where(s => !string.IsNullOrEmpty(s)).ToArray();//清理空数组
            var path_xiangduis = Path.GetDirectoryName(xiangdui).Split('\\');
            //判断向上删除几个
            int inx = 0;
            foreach (var item in path_xiangduis)
            {
                if (item == "..")
                {
                    inx++;
                }
                else
                {
                    break;
                }
            }
            //前段
            var pathA = new StringBuilder();
            for (int i = 0; i < path_dwgs.Length - inx; i++)
            {
                pathA.Append(path_dwgs[i] + "\\");
            }
            //后段
            var pathB = new StringBuilder();
            foreach (var item in path_xiangduis)
            {
                if (item != "." && item != "..")
                {
                    pathB.Append(item + "\\");
                }
            }
            if (!string.IsNullOrEmpty(xiangdui) && xiangdui[0] == '.')
            {
                re_path_opend = pathA.ToString() + pathB.ToString() + Path.GetFileName(xiangdui);
            }
            else
            {
                re_path_opend = xiangdui;
            }
            return re_path_opend;
        }
    }
}
